function getHuDu(r,num,r2){
	if (num=='') return {zb:[-100,-100]};
	//var r = 190/2;//半径
	r = r * 0.75;
	var a=r,b=r;//圆心坐标,position:absolute,left:a,top:b
	var jd = 225+(num/100*270);//225度是起点，后面总共可用区域有270度，计算它的百分比
	var hudu = (2*Math.PI / 360) * jd;
   	var _x = a + Math.sin(hudu) * r + r2*1.25;      //根据三角形的正玄、余弦来得到圆心x,y轴坐标
   	var _y = b - Math.cos(hudu) * r + r2*1.25;    //注意此处是“-”号，因为我们要得到的Y是相对于（0,0）而言的
   	//-12 -12.5为矩形的宽高的一半，即图标的xy坐标轴校正偏移量
	var _styles1 = 'left:'+_x+'px;top:'+(_y)+'px;z-index:11;';
	//jd += 225;
	return {
		zb:[_x,_y],
		jd:jd
	};
}
$(function(){
	var pageSize = getPageSize();
	var temp_option = {temp:[]};
	var option;
	$('#chart').css({
		width:(chart_type=='gauge'?pageSize[3]:pageSize[2])+'px',
		height:pageSize[3]+'px'
	})
	var $scope = {data:{
 		fieldDate: '',
 		chart_x: [],
 		chart_y: [],
 		chartType: [],
 		xData: [],
 		mychart: '',
 		pieseries: [],
 		lineseries: [],
 		barseries: [],
 		mapseries:[],
 		gauseries:[],
 		legendData: [],
 		searchData: [],
 		searchList: []
 	}};
	$scope.class22 = {
		chart_type: chart_type,
		show_edit: false
	};
 	$scope.initEcharts = function(){
		if(chart_type=='gauge'){
			$('#chart').addClass('chart-wrap-gau');
			$('#chart').css({
				"width":pageSize[3]+"px"
			});
		}
		$scope.data.mychart = echarts.init(document.getElementById("chart"),'macarons');
		if($scope.class22.chart_type == 'line'){
			option = {
				tooltip: {
			        trigger: 'axis',
			        axisPointer :{
			        	type:"shadow"
			        }
			    },
				legend: {
					top: '5%',
			        data: $scope.data.legendData
			    },
			    xAxis: {
			        type: 'category',
			        boundaryGap: false,
			        data: $scope.data.xData
			    },
			    yAxis: {
			        type: 'value'
			    },
			    series:$scope.data.lineseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);		
		}else if($scope.class22.chart_type == 'bar'){
			option = {
				tooltip: {
			        trigger: 'axis',
			        axisPointer :{
			        	type:"shadow"
			        }
			    },
				legend: {
					top: '5%',
			        data: $scope.data.legendData
			    },
			    xAxis: {
			        type: 'category',
			        data: $scope.data.xData,
			        axisTick: {
		                alignWithLabel: true
		            }
			    },
			    yAxis: {
			        type: 'value'
			    },
			    series:$scope.data.barseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);
		}else if($scope.class22.chart_type == 'pie'){
			option = {
			    legend: {
			        orient: 'vertical',
			        itemWidth: 5,
			        itemHeight: 5,
			        x: 'right',
			        align: 'left',
			        data: $scope.data.xData
			    },
			    series: $scope.data.pieseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);
		}else if($scope.class22.chart_type == 'map'){
			var mapName = $.trim($scope.data.chart_x[0].title);
			var mapPinYin = pinyin.getFullChars(mapName).toLowerCase();
			var mapNamePath = config.map2code[mapName];
			
			if(!mapNamePath){
				return false;
			}
			
			//注册地图
			getJSONData(baseUrl+'/chart_html/chart_asset/map/compressed/'+mapNamePath, {}, function(returnJson){
				echarts.registerMap(mapPinYin, returnJson);
				option = {
					tooltip: {
				    	show:true,
				        trigger: 'item',
				        textStyle:{
	                    	fontSize:14
	                  	}
				    },
				    visualMap: {
				    	type:'continuous',
				        min: 80,
				        max: 550,
				        left: '5%',
	    				bottom: '5%',
				        right: 'auto',
				        top: 'auto',
	    				text: ['高','低'],
				        show :true,
				        inRange :{
				        	color:['#61aceb','#7abdef','#a0d9f6','#c7eefb','#dbfcfe'].reverse()
				        }
				    },
				    series: [
				        {
				            name: mapName,//'江苏',
				            type: 'map',
				            map: mapPinYin,//'jiangsu',
				            selectedMode :'single',
				            left:'50px',
				            right:'auto',
				            bottom:'10px',
				            top:'10px',
				            label: {
				                normal: {
				                    show: true,
				                    textStyle:{
				                    	fontSize:12
				                    }
				                },
				                emphasis: {
				                    show: true,
				                    textStyle:{
				                    	fontSize:13
				                    }
				                }
				            },
				            itemStyle:{
				            	normal:{
				            		borderColor:'#333'
				            	}
				            },
				            data:$scope.data.mapseries
				        }
				    ]
				    //series: $scope.data.mapseries
				};
				temp_option.temp = [option];
				$scope.data.mychart.setOption(option);
			})
		}else if($scope.class22.chart_type == 'gauge'){
			var gaTitle = $.trim($scope.data.chart_x[0].title);
			var tempHuDu = getHuDu(pageSize[3]/2, yuqi, pageSize[3]/10);
			//console.log(tempHuDu);
			option = {
			   	series: [{
			        min:0,
		            type: 'gauge',
		            name: '仪表盘',
		            radius:'75%',
		            splitNumber :6,
		            max:100,
		            grid:{
		            	left:"10%"
		            },
		            title:{
		            	offsetCenter :[0,'-30%'],
		            	textStyle:{
		            		color:"#fff"
		            	}
		            },
		            axisLine:{
		            	lineStyle:{
		            		color:[[(fenduan1/100).toFixed(2), "#44d641"], [(fenduan2/100).toFixed(2), "#e8dc20"], [1, "#dd0606"]],
		            		width:14
		            	}
		            },
		            splitLine :{
		            	length :25,
		            	lineStyle:{
		            		color:'#102774'
		            	}
		            },
		            axisTick :{
		            	splitNumber :6,
		            	lineStyle:{
		            		color:'#102774'
		            	}
		            },
		            axisLabel :{
		            	show:false
		            },
		            pointer :{
		            	width:3,
		            	length:'70%'
		            },
		            itemStyle :{
		            	normal :{
		            		color :'#dbfeff',
		            		borderColor :'#22bae4',
		            		borderWidth :1
		            	}
		            },
		            detail: {
		            	offsetCenter:[0,'74%'],
		            	textStyle:{
		            		color:'auto',//'#555555',
		            		fontSize:20,
		            		fontWeight:'200'
		            	},
		            	formatter: function (value) {
						    return value.toFixed(2)+'%';
						}
		            },
		            markPoint:{
		            	symbol:'pin',
		            	symbolSize :pageSize[3]/10,
		            	symbolRotate :0,//tempHuDu.jd,
		            	//symbolOffset :[50,50],
		            	label:{
		            		normal:{
		            			formatter:function(params){
		            				return '预';
		            			}
		            		}
		            	},
		            	data:[{
		            		x:tempHuDu.zb[0].toFixed(0),
		            		y:tempHuDu.zb[1].toFixed(0),
		            		itemStyle:{
		            			normal:{
		            				color:'#1487d3'
		            			}
		            		}
		            	}]
		            },
		            data: $scope.data.gauseries
			    }]
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);
		}
	}
 	
 	var vd = [];
 	$.each(chart_x, function(iii,eee) {
 		vd.push(eee.fieldName)
 	})
 	vd = JSON.stringify(vd);
 	
 	//重新取值
	$.each(chart_x, function(ii,e) {
		getResult(e.viewName,e.fieldName,vd,function(bk){
			chart_x[ii].data = bk;
			$.each(chart_y, function(jj,f) {
				getResult(f.viewName,f.fieldName,vd,function(bk2){
					chart_y[jj].data = bk2;
					$scope.data.chart_x = chart_x;
					$scope.data.chart_y = chart_y;
					if($scope.data.chart_x[0]){
						$scope.data.xData = $scope.data.chart_x[0].data;
					}
					if($scope.data.lineseries){
						$scope.data.lineseries = [];
					}
					if($scope.data.barseries){
						$scope.data.barseries = [];
					}
					if($scope.data.pieseries){
						$scope.data.pieseries = [];
					}
					for (var i = 0; i < $scope.data.chart_y.length; i++) {
						$scope.data.legendData.push($scope.data.chart_y[i].title);
						$scope.data.lineseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'line',
				            data: $scope.data.chart_y[i].data
						});
						
						$scope.data.barseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'bar',
				            data: $scope.data.chart_y[i].data
						});
						
						var userName = [], userValue = [] ,pieData = [];
							userName = $scope.data.chart_x[0].data;
							userValue = $scope.data.chart_y[i].data;
						for(var j = 0; j< userName.length; j++){
							pieData.push({
								value: userValue[j],
								name: userName[j]
							});
						}
						$scope.data.pieseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'pie',
				            radius: ['50%', '70%'],
				            label: {
				                normal: {
				                    show: false,
				                    position: 'center'
				                },
				                emphasis: {
				                    show: true,
				                    textStyle: {
				                        fontSize: '30',
				                        fontWeight: 'bold'
				                    }
				                }
				            },
				            data: pieData
						});
						
						var temp_map_data = [];
						var mapName = $.trim($scope.data.chart_x[0].title);
						if(mapName=='江苏省'){
							temp_map_data = [
				            	{name: '苏州市',value:545.4 },
				            	{name: '南京市',value:337.8 },
				            	{name: '无锡市',value:274.9 },
				            	{name: '南通市',value:189 },
				            	{name: '徐州市',value:168.8 },
				            	{name: '盐城市',value:161.1 },
				            	{name: '常州市',value:145.6 },
				            	{name: '淮安市',value:120 },
				            	{name: '宿迁市',value:107.1 },
				            	{name: '泰州市',value:104.1 },
				            	{name: '连云港市',value:100.9 },
				            	{name: '镇江市',value:94.1 },
				            	{name: '扬州市',value:83.7 }
				            ]
						}
						if(mapName=='南通县区'){
							temp_map_data = [
				            	{name: '海安县',value:545.4 },
				            	{name: '如皋市',value:337.8 },
				            	{name: '如东县',value:274.9 },
				            	{name: '通州区',value:189 },
				            	{name: '港闸区',value:168.8 },
				            	{name: '崇川区',value:104.1 },
				            	{name: '市开发区',value:100.9 },
				            	{name: '海门市',value:94.1 },
				            	{name: '启东市',value:83.7 }
				            ]
						}
						$scope.data.mapseries = temp_map_data;
						
						if(mapName=='地区生产总值增长速度'){
							temp_map_data = [{value:12.9,name:mapName}]
						}else if(mapName=='一般公共预算收入增长速度'){
							temp_map_data = [{value:45.7,name:mapName}]
						}else if(mapName=='固定资产增长速度'){
							temp_map_data = [{value:60.7,name:mapName}]
						}else if(mapName=='社会消费品零售总额增速'){
							temp_map_data = [{value:91.0,name:mapName}]
						}
						$scope.data.gauseries = temp_map_data;
					}
					
					if($scope.data.chart_x != '' && $scope.data.chart_y != ''){
						$scope.initEcharts();
					}
				})
			});
		})
	});
})